package myservice.android.activities;

import myservice.android.Global;
import myservice.android.R;
import myservice.android.utilities.GraphCanvas;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ReportsGraphsActivity extends Activity {
	
	private TextView range, legend;
	private Button customizeButton;
	private GraphCanvas canvas;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.reports_graphs);           
        
        range = (TextView) findViewById(R.id.rangeLabel);
        customizeButton = (Button) findViewById(R.id.customizeButton);
        canvas = (GraphCanvas) findViewById(R.id.canvas);
        legend = (TextView) findViewById(R.id.legendText);
        
        customizeButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(getApplicationContext(), CustomizeGraphsActivity.class);			
				startActivity(i);		
			}        
        });
        
	}
    
    @Override
    public void onResume() {
    	super.onResume();
    	updateUi();
    }
    
    private void updateUi() {
    	range.setText(Global.monthNames[Global.fromMonth]+" "+Global.fromYear+" - "+Global.monthNames[Global.toMonth]+" "+Global.toYear);
    	
    	SpannableStringBuilder legendText = new SpannableStringBuilder("");    	
		
    	// Construct the legend text and apply appropriate colors
    	if (Global.showTotalTimeGraph==true)
    		appendColoredText(legendText,Global.res.getString(R.string.label_graph_total_time),0xFFFFFF00);
    		
    	if (Global.showMinistryTimeGraph==true)
    		appendColoredText(legendText,Global.res.getString(R.string.label_graph_ministry_time),0xFFFF7F27);  
    	
    	if (Global.showCreditTimeGraph==true)
    		appendColoredText(legendText,Global.res.getString(R.string.label_graph_credit_time),0xFF3F48CC);
    	
    	if (Global.showMagazinesGraph==true)
    		appendColoredText(legendText,Global.res.getString(R.string.label_graph_magazines),0xFF00A2E8);  
    	
    	if (Global.showBooksGraph==true)
    		appendColoredText(legendText,Global.res.getString(R.string.label_graph_books),0xFF22B14C);     	
    	
    	if (Global.showBrochuresGraph==true)
    		appendColoredText(legendText,Global.res.getString(R.string.label_graph_brochures),0xFFA349A4);
    	
    	if (Global.showTractsGraph==true)
    		appendColoredText(legendText,Global.res.getString(R.string.label_graph_tracts),0xFFC3C3C3);   
    	
    	if (Global.showVideosGraph==true)
    		appendColoredText(legendText,Global.res.getString(R.string.label_graph_videos),0xFFC8BFE7);    	
    	
    	if (Global.showReturnCallsGraph==true)
    		appendColoredText(legendText,Global.res.getString(R.string.label_graph_return_calls),0xFFB97A57); 
    	
    	if (Global.showBibleStudentsGraph==true)
    		appendColoredText(legendText,Global.res.getString(R.string.label_graph_bible_students),0xFFFF0000);     	      	    	
    	
    	legend.setText(legendText);
    	
    	canvas.invalidate();
    }
    
    // Appends a String and applies the specified color to it
    private static void appendColoredText(SpannableStringBuilder originalText, String appendedText, int color) {
    	if (appendedText.length()==0)
    		return;
    	if (originalText.length()>0)
    		originalText.append("  ");
    	int originalLength = originalText.length();
    	originalText.append(appendedText);
    	originalText.setSpan(new ForegroundColorSpan(color), originalLength, originalText.length(), 0); 
    }       

}